from flask import current_app, request
import jwt
from utilsapp.mysql_con import db_insert


def add_log(title, msg, type='ERROR'):
    title = title
    content = msg
    url = request.url
    token = '' if 'Authorization' not in request.headers else request.headers['Authorization']
    key = current_app.config['settings']['SECRET_KEY']
    user_name = ''
    try:
        info = jwt.decode(token, key, algorithms='HS256')
        user_name = info['user_name']
    except:
        pass

    add_model = {
        "type": type,
        "url": url,
        "filename": '',
        "content": content,
        "title": title,
        "user_name": user_name
    }
    db_insert("log", add_model)
